package com.example.bidding.web;

import com.example.bidding.service.BorrowService;
import com.example.common.Ov.UserOv;
import com.example.common.entity.Const;
import com.example.common.entity.Rsp;
import com.example.common.pojo.Borrow;
import com.example.common.pojo.RepayRecord;
import com.example.common.pojo.User;
import com.example.common.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 放标
 */
@RestController
public class BorrowController {

  @Autowired
  private BorrowService borrowService;

  //添加借钱
  @PostMapping("add")
    public Rsp add(@RequestHeader("Authorization")String token,@RequestBody Borrow borrow){
    //System.out.println(borrow);
    UserOv userOv= JWTUtil.parserToken(token, Const.PUBLIC_KEY);
    //System.out.println(userOv);
     return borrowService.add(userOv, borrow);
    }

    //查看还有多少钱没有还完和总的还款的时间
  @GetMapping("getBorrowDet")
  public Rsp getBorrowDet(@RequestHeader("Authorization")String token){
    UserOv userOv= JWTUtil.parserToken(token, Const.PUBLIC_KEY);
    return borrowService.getBorrowDet(userOv);
  }

  //获取用户的信息包含额度
  @GetMapping("getUserInfo2")
  public Rsp getUserInfo2(@RequestHeader("Authorization") String token){
    return borrowService.getUserInfo(token);
  }

  //获取用户的还款详情
  @GetMapping("getRepayRecord")
  public Rsp getRepayRecord(@RequestHeader("Authorization") String token){
    return borrowService.getRepayRecord(token);
  }

  //获取还款详情
  @PostMapping("getRepayDetilByRepayRecordId")
  public Rsp getRepayDetilByRepayRecordId(@RequestHeader("Authorization")String token,@RequestBody RepayRecord repayRecord){
    return borrowService.getRepayDetilByRepayRecordId(repayRecord);
  }

  //获取借钱的记录
  @GetMapping("getBorrowRecord")
  public Rsp getBorrowRecord(@RequestHeader("Authorization")String token){
    UserOv userOv= JWTUtil.parserToken(token, Const.PUBLIC_KEY);
    return borrowService.getBorrowListByUserId(userOv.getId());
  }

  //获取用户的还款详情通过userId和borrowId两个参数获取
  @PostMapping("getRepayRecordByBorrowId")
  public Rsp getRepayRecordByBorrowId(@RequestHeader("Authorization") String token,@RequestBody Borrow borrow){
    return borrowService.getRepayRecordByBorrowId(borrow.getId());
  }

  //获取还款记录
  @GetMapping("getAllRepayRecord")
  public Rsp getRepayRecordByBorrowId(@RequestHeader("Authorization") String token){
    UserOv userOv= JWTUtil.parserToken(token, Const.PUBLIC_KEY);
      return borrowService.getRepayRecordByUserId(userOv.getId());
  }


  //获取未还款的记录(
  @GetMapping("getLoanList")
  public Rsp getLoanList(@RequestHeader("Authorization") String token){
    UserOv userOv= JWTUtil.parserToken(token, Const.PUBLIC_KEY);
    return borrowService.getRepayRecordByUserId2(userOv.getId());
  }


  //获取被选中的还款的金额和修改是否被选中
  @PostMapping("getCheckMoney")
  public Rsp getCheckMoney(@RequestHeader("Authorization")String token,@RequestBody RepayRecord repayRecord){
    UserOv userOv= JWTUtil.parserToken(token, Const.PUBLIC_KEY);
    return borrowService.getCheckMoney(repayRecord,userOv.getId());
  }


  //支付是否成功
  @PostMapping("pay")
  public Rsp pay(@RequestHeader("Authorization")String token,@RequestBody User user){
    return borrowService.pay(token,user);
  }
}
